package edu.guigu.serverservice;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import edu.guigu.serverservice.databinding.ActivityMainBinding;
import edu.guigu.serverservice.local.BindService;
import edu.guigu.serverservice.local.ForegroundService;
import edu.guigu.serverservice.local.SimpleService;

// 服务端Serivce
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityMainBinding binding;

    private BindService bindService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        binding.btnStart.setOnClickListener(this);
        binding.btnStop.setOnClickListener(this);
        binding.btnBind.setOnClickListener(this);
        binding.btnUnbind.setOnClickListener(this);
        binding.btnForeground.setOnClickListener(this);
        binding.btnForegroundStop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_start) {
            // 启动服务
            Intent intent = new Intent(this, SimpleService.class);
            startService(intent);
        }
        if (id == R.id.btn_stop) {
            // 停止服务
            Intent intent = new Intent(this, SimpleService.class);
            stopService(intent);
        }
        if (id == R.id.btn_bind) {
            // 绑定服务
            Intent intent = new Intent(this, BindService.class);
            bindService(intent, serviceConnection, BIND_AUTO_CREATE);
        }
        if (id == R.id.btn_unbind) {
            // 解绑服务
            unbindService(serviceConnection);
            // TODO: 测试，获取服务里的数据，到UI上展示
            binding.tvMsg.setText("");
        }
        // 启动前台服务
        if (id == R.id.btn_foreground) {
            Intent intent = new Intent(this, ForegroundService.class);
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
                startActivity(intent);
            } else {
                startForegroundService(intent);
            }
        }
        if (id == R.id.btn_foreground_stop) {
            Intent intent = new Intent(this, ForegroundService.class);
            stopService(intent);
        }
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        // 绑定服务成功时调用
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BindService.MyBinder myBinder = (BindService.MyBinder) service;
            bindService = myBinder.getService();
            // TODO: 测试，获取服务里的数据，到UI上展示
            binding.tvMsg.setText(bindService.getMessage());
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
}